package org.admanager.soa.plugin.popup.actions;

import java.util.Iterator;

import org.admanager.soa.plugin.util.KermetaRunner;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import fr.irisa.triskell.eclipse.console.EclipseConsole;
import fr.irisa.triskell.eclipse.console.IOConsole;

public class PatternMetaModelInitActionDelegate implements
		IObjectActionDelegate {
	private ISelection selection;
	public PatternMetaModelInitActionDelegate() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run(IAction action) {
		// TODO Auto-generated method stub
		if (selection instanceof IStructuredSelection) {
			Iterator iter = ((IStructuredSelection) selection).iterator();
			while (iter.hasNext()) {
				Object obj = (Object) iter.next();
				if (obj instanceof IFile)
				{
					//Get file
					IFile inputModelFile = (IFile)obj;
					URI inputModel = URI.createURI(inputModelFile.getLocationURI().toString());
					//Get the current project
					IProject myProject = inputModelFile.getProject();

					//Get the URI for the output model
					
					IFolder patternViewFolder = myProject.getFolder("PatternView");
					if (!patternViewFolder.exists())
						try {
							patternViewFolder.create(true, true, new NullProgressMonitor());
						} catch (CoreException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					
					IFolder patternFolder = patternViewFolder.getFolder(inputModelFile.getName().replace("soapattern", ""));
					if (!patternFolder.exists())
						try {
							patternFolder.create(true, true, new NullProgressMonitor());
						} catch (CoreException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
			        
					URI outputModel = URI.createURI(patternFolder.getLocationURI()
								+ "/"
								+ inputModel.lastSegment().replace("soapattern",
										"ecore"));
					//Run Kermeta
					IOConsole console = new EclipseConsole("Kermeta Simulator");
					try {						
						String sourcePatternUrl = inputModel.toString();
						String targetPatternUrl = outputModel.toString();
						
						KermetaRunner.Pattern2View(sourcePatternUrl, targetPatternUrl, console);
						
					} catch (Throwable e) {
						
						e.printStackTrace();
					}
					//End Run Kermeta
					try {
						myProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor() );
					} catch (CoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		this.selection = selection;
		action.setEnabled(!selection.isEmpty());
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub

	}

}
